package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.FamliyItemBeen;
import cn.xfdzx.android.apps.shop.contract.FamilyContract;
import cn.xfdzx.android.apps.shop.model.FamilyModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class FamilyPresenter extends BasePresenter<FamilyContract.View> implements FamilyContract.Presenter {

    private FamilyContract.Model model;

    public FamilyPresenter() {
        model = new FamilyModel() ;
    }


    @Override
    public void initData(boolean isRefresh, boolean isLoading, String page, String limit) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.initData(page,limit)
                .compose(RxScheduler.<FamliyItemBeen>Flo_io_main())
                .as(mView.<FamliyItemBeen>bindAutoDispose())
                .subscribe(new Consumer<FamliyItemBeen>() {
                    @Override
                    public void accept(FamliyItemBeen bean) throws Exception {
                        mView.onInitSuccess(bean);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }

                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void addCar(String is_new, String uniqueId, String productId, String cartNum,int position,int total_num) {
        if (!isViewAttached()) {
            return;
        }
//        mView.showLoading();
        model.addCar(is_new,uniqueId,productId,cartNum) .compose(RxScheduler.<ConfirmGoodBeen>Flo_io_main())
                .as(mView.<ConfirmGoodBeen>bindAutoDispose())
                .subscribe(new Consumer<ConfirmGoodBeen>() {
                    @Override
                    public void accept(ConfirmGoodBeen bean) throws Exception {
                        mView.onAddCarSuccess(bean,  position,  total_num);
//                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
//                        mView.hideLoading();
                    }
                });
    }

}
